package sort;

import java.util.Arrays;

public class RadixSort {
    public static void main(String[] args) {
        int[] arr = new int[]{542,53,3,14,214,748};
      //  radixSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    private static void radixSort(int[] arr){
        int max=arr[0];
        for(int i =0;i<arr.length;i++){
            if(arr[i]>max){
                max=arr[i];
            }
        }
        int maxLength=(max+"").length();
        int[][] bucket = new int[10][arr.length];
        int[] backElementCounts = new int[10];
        int num =0;
        for(int x =0 ,y=1;x<maxLength; x++,y*=10){
            //第一轮 按照桶排序存入桶中
            for (int i = 0; i < arr.length; i++) {
                num=arr[i]/y%10;
                bucket[num][backElementCounts[num]]=arr[i];
                backElementCounts[num]++;
            }
            //按照桶的顺序依次取出放入原数组中
            int index=0;
            for (int i = 0; i < backElementCounts.length; i++) {
                if(backElementCounts[i]!=0){
                    for (int j=0;j<backElementCounts[i];j++){
                        arr[index]=bucket[i][j];
                        index++;
                    }
                }
                backElementCounts[i]=0;
            }
        }
      /*  //第一轮 按照桶排序存入桶中
        for (int i = 0; i < arr.length; i++) {
            num=arr[i]%10;
            bucket[num][backElementCounts[num]]=arr[i];
            backElementCounts[num]++;
        }
        //按照桶的顺序依次取出放入原数组中
        int index=0;
        for (int i = 0; i < backElementCounts.length; i++) {
            if(backElementCounts[i]!=0){
                for (int j=0;j<backElementCounts[i];j++){
                    arr[index]=bucket[i][j];
                    index++;
                }
            }
           backElementCounts[i]=0;
        }
        //第二轮
        for (int i = 0; i < arr.length; i++) {
            num=arr[i]/10%10;
            bucket[num][backElementCounts[num]]=arr[i];
            backElementCounts[num]++;
        }
        //按照桶的顺序依次取出放入原数组中
         index=0;
        for (int i = 0; i < backElementCounts.length; i++) {
            if(backElementCounts[i]!=0){
                for (int j=0;j<backElementCounts[i];j++){
                    arr[index]=bucket[i][j];
                    index++;
                }
            }
        }*/
    }
}
